package cn.picclife.mwx.manager.sys.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.picclife.mwx.manager.base.BaseResult1;
import cn.picclife.mwx.manager.sys.model.bo.UserSessionBo;
import cn.picclife.mwx.manager.sys.model.po.FlowPath;
import cn.picclife.mwx.manager.sys.service.DictionaryValueService;
import cn.picclife.mwx.manager.sys.service.FlowPathService;
import cn.picclife.mwx.manager.util.Num8Util;
import cn.picclife.mwx.manager.util.RestUtils;

/**
 * @ClassName FlowPathController.java
 * @Description 流程基本信息控制层
 * @author Guan JunHui
 * @Date 2017年9月13日
 */
@Controller
@RequestMapping(value = "/api")
public class FlowPathController {
	
	
	@Autowired
	private FlowPathService service;
	
	@Autowired
	private DictionaryValueService dictionaryValueService;
	
	/**
	 * 获取登录用户公司
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/flowpath/to_flowpath")
	@ResponseBody
	public Map<String, Object> toFlowpath(HttpServletRequest request, HttpServletResponse response) throws Exception{
		UserSessionBo userSessionBo = (UserSessionBo) request.getSession().getAttribute("user");
		Map<String, Object>map = new HashMap<String,Object>();
		int num = getId(99999999, 0);
		map.put("company", userSessionBo.getOptName());
		map.put("id", num);
		map.put("province", dictionaryValueService.queryDictionary("province"));
		map.put("insurance", dictionaryValueService.queryDictionary("insurance"));
		return map;
	}
	
	@RequestMapping(value = "/flowpath/add_flowpath")
	@ResponseBody
	public BaseResult1 insertFlowPath(FlowPath flowPath,HttpServletRequest request, HttpServletResponse response){
		BaseResult1 result = new BaseResult1();
		if ("".equals(flowPath.getFlowpath_id())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录节点编码不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if ("".equals(flowPath.getNum())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录节点个数不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (StringUtils.isBlank(flowPath.getName())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录节点名称不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if ("".equals(flowPath.getArea())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录节点适用地区不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if ("".equals(flowPath.getInsurance_type())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录节点适用险类不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (flowPath.getStartDate() == null) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录节点生效起期不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (flowPath.getEndDate() == null) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录节点生效止期不能为空");
			RestUtils.error(response, result);
			return null;
		}
/*		Map<String, Object>map = service.queryFlowPath(flowPath);
		if (flowPath.getName().equals(map.get(flowPath.getName()))) {
			result.setRet(BaseResult.BIZ_ERROR);
			result.setMsg("该双录节点名称已经存在!");
			RestUtils.error(response, result);
			return null;
		}*/
		 UserSessionBo user = (UserSessionBo) request.getSession().getAttribute("user");
		 flowPath.setCreateUser(user.getOptName());
		int  flag = service.insertFlowPath(flowPath);
		if (flag >0) {
			result.setRet(String.valueOf(flowPath.getFlowpath_id()));
			result.setMsg("操作成功");
			return result;
		}else{
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("操作失败");
			return null;
		}
	}
	
	
	@RequestMapping(value = "/flowpath/update_FlowPath")
	@ResponseBody
	public BaseResult1 updateFlowPath(FlowPath flowPath,HttpServletRequest request, HttpServletResponse response){
		BaseResult1 result = new BaseResult1();
		if (StringUtils.isBlank(flowPath.getName())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录节点名称不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if ("".equals(flowPath.getArea())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录节点适用地区不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if ("".equals(flowPath.getInsurance_type())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录节点适用险类不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (flowPath.getStartDate() == null) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录节点生效起期不能为空");
			RestUtils.error(response, result);
			return null;
		}
		if (flowPath.getEndDate() == null) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录节点生效止期不能为空");
			RestUtils.error(response, result);
			return null;
		}
	/*	FlowPath other = service.queryFlowPathForUpdate(flowPath);
		if (other != null) {
			result.setRet(BaseResult.BIZ_ERROR);
			result.setMsg("该双录节点名称已经存在");
			RestUtils.error(response, result);
			return null;
		}*/
		
		Map<String, Object>map = service.searchFlowPathById(flowPath.getFlowpath_id());
		boolean flag = ((int)map.get("area")==flowPath.getArea())&&true;
		flag =flag && (map.get("endDate").equals(flowPath.getEndDate()));
		flag = flag && ((int)map.get("insurance_type")==flowPath.getInsurance_type());
		flag = flag && (map.get("name").equals(flowPath.getName()));
		flag = flag && ((int)map.get("num") == flowPath.getNum());
		flag = flag && (map.get("rule").equals(flowPath.getRule()));
		flag = flag && (map.get("startDate").equals(flowPath.getStartDate()));
		if (flag) {
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("修改成功");
			return result;
		}
		
		 UserSessionBo user = (UserSessionBo) request.getSession().getAttribute("user");
		 flowPath.setEditUser(user.getOptName());
		int num = service.updateFlowPath(flowPath);
		if (num >0) {
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("修改成功");
			return result;
		}else{
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("修改失败");
			return result;
		}
	}
	
	@RequestMapping(value = "/flowpath/search_FlowPath")
	@ResponseBody
	public Map<String, Object> searchFlowPath(int flowPath_Id){
		return service.searchFlowPathById(flowPath_Id);
	}
	
	@ResponseBody
	@RequestMapping(value = "/flowpath/delete_FlowPath")
	public BaseResult1 deleteFlowPath(@RequestParam("flowPath_Id")String flowPath_Id){
		BaseResult1 result = new BaseResult1();
		if (StringUtil.isBlank(flowPath_Id)) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("请选择后在点击删除按钮");
			return result;
		}
		String[]flowPath_Ids = flowPath_Id.split(",");
		int[]ids = new int[flowPath_Ids.length];
		for (int i = 0; i < ids.length; i++) {
			ids[i] = Integer.valueOf(flowPath_Ids[i]);
		}
		int num = service.deleteFlowPath(ids);
		if (num == 0) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("删除失败");
			return result;
		}
		result.setRet(BaseResult1.SUCCESS);
		result.setMsg("删除成功");
		return result; 
	}
	
	@ResponseBody
	@RequestMapping(value = "/flowpath/query_list")
	public Map<String, Object> pageFlowPath(FlowPath flowPath){
		return service.queryFlowPath(flowPath);
	}
	
	/**
	 * 获取id方法
	 * @param total
	 * @param flowPath
	 * @param id
	 * @return
	 */
	private int getId(int total,int id) throws Exception{
		if (id<10000000) {
			id = Num8Util.getNumner();
		}
		Map<String, Object>map = service.searchFlowPathById(id);
		if (map == null) {
			return id;
		}else{
			id = Num8Util.getNumner();
			return getId(total-1, id);
		}
	}
	
}
